<?php

// +----------------------------------------------------------------------
// | 版权所有 2017-2027 云南环拓科技有限公司，并保留所有权利。
// +----------------------------------------------------------------------
// | Copyright (c) 2017-2027 http://www.yn123.com All rights reserved.
// +----------------------------------------------------------------------
// | Notice: This code is not open source, it is strictly prohibited
// |         to distribute the copy, otherwise it will pursue its
// |         legal responsibility.
// | 注意：  这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用,
// |        不允许对程序代码以任何形式任何目的的再发布。
// +----------------------------------------------------------------------
// | Author: feraner <feraner@163.com> 
// | Date: 2020/1/3
// +----------------------------------------------------------------------

namespace app\common\services\statistics;

use app\common\facade\Arr;
use library\tools\Data;
use think\Db;

/**
 * 个人中心模块分析
 * Class UserHandleService
 * @package app\common\services\statistics
 * @author feraner<feraner@163.com>
 * @date 2020/1/3
 */
class UserHandleService extends BasicHandleService
{

    /**
     * 用户中心 api
     * @var string
     */
    protected $apiUserSuffix = 'user';

    /**
     * 用户中心 web
     * @var string
     */
    protected $webUserSuffix = 'usercenter';



    public function __construct()
    {
        parent::__construct();
    }


    /**
     * @param $handleData
     *
     * @return mixed|void
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function handle($handleData)
    {

        if ($this->isHandle($handleData)) {
            //是否需要增加UV
            $this->addUv = $this->addUvNum(
                Arr::get($handleData, 'userId'),
                Arr::get($handleData, 'logId')
            );


            //用户模块
            $this->__handleUserModule($handleData);

        }
    }


    /**
     * 处理用户模块点击
     * @param $handleData
     *
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    private function __handleUserModule($handleData)
    {
        if ($this->isWeb) {

            $dayData = [
                'type'          => 1,
                'user_click'     => Db::raw('user_click + 1'),
                'start_time'    => $this->todayStart,
                'end_time'      => $this->todayEnd,
            ];
            if ($this->addUv) {
                $dayData['user_uv'] = Db::raw('user_uv + 1');
            }

            if ($statId = $this->checkModuleStatExit($dayData)) {
                $dayData['id'] = $statId;
            }

            Data::save('behavior_module_stat', $dayData);

        }

    }








    /**
     * 确认是否是该模块需要处理
     * @param $handleData
     *
     * @return bool|mixed
     */
    protected function isHandle($handleData)
    {
        $this->isWeb = false;
        $this->isApi = false;

        //api 接口
        if (Arr::valueEq($handleData['urlArr'], 0, $this->apiSuffix) &&
            Arr::valueEq($handleData['urlArr'], 1, $this->apiUserSuffix)) {
            $this->isApi = true;
            return true;
        }

        //web界面
        if (Arr::valueEq($handleData['urlArr'], 0, $this->webSuffix) &&
            Arr::valueEq($handleData['urlArr'], 1, $this->webUserSuffix)) {
            $this->isWeb = true;
            return true;
        }

        return false;
    }


    public function handleMonthAndTotal()
    {
        //无须处理
        return true;
    }

}
